package com.chuangke.admin.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.entity.SysDictType;
import com.chuangke.admin.service.SysDictTypeService;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;

/**
 * 字典类型Controller
 *
 * @author chuangke
 * @date "2020-07-21"
 */
@RestController
@RequestMapping("/admin/dictType")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 查询字典类型列表
     */
    @PreAuthorize("hasAuthority('login:action')")
    @GetMapping("/list")
    public HttpResult list(String name) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        CkPageQuery query = new CkPageQuery(params);

        return HttpResult.ok(sysDictTypeService.page(query));
    }

    @PreAuthorize("hasAuthority('login:action')")
    @PostMapping("/page")
    public HttpResult page(@RequestBody Map<String, Object> params) {
        return HttpResult.ok(sysDictTypeService.page(new CkPageQuery(params)));
    }

    /**
     * 获取字典类型详细信息
     */
    @PreAuthorize("hasAuthority('login:action')")
    @GetMapping(value = "/{code}")
    public HttpResult getInfo(@PathVariable("code") String code) {
        return HttpResult.ok(sysDictTypeService.findByType(code));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("hasAuthority('login:action')")
    @PostMapping
    public HttpResult add(@RequestBody SysDictType sysDictType) {
        sysDictTypeService.save(sysDictType);
        return HttpResult.ok();
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("hasAuthority('login:action')")
    @PutMapping
    public HttpResult edit(@RequestBody SysDictType sysDictType) {
        sysDictTypeService.save(sysDictType);
        return HttpResult.ok();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("hasAuthority('login:action')")
    @DeleteMapping("/{codes}")
    public HttpResult remove(@PathVariable String[] codes) {
        sysDictTypeService.deleteByType(Arrays.asList(codes));
        return HttpResult.ok();
    }
}
